#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_NTP_002(Case):
    """
        Name:
            REDFISH_NTP_002
        Label:
            查询用户资源信息
        PreCondition:
            BMC web service normal
        TestStep:
            1) PATCH https://BMC_IP/redfish/v1/Managers/1/NtpService，body里添加{
                "PreferredNtpServer": "ntp.jd.local",
                "AlternateNtpServer": "ntp.ark.jdcloud.com",
                "ExtraNtpServer": "bmctime.jd.com"
                 }
                需要get获取If-Match: ETag_value
        ExpectedResult:
            A) 执行响应码为200,获取修改信息和ibmc-时区&NTP ，ntp信息一致
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询NTPService且获取header信息")
        url = "/redfish/v1/Managers/1/NtpService"

        header, rc, content = self.request.get_header_detail(url)
        if_match_str = header['ETag']
        self.request.headers['If-Match'] = if_match_str
        PreferredNtpServer = content['PreferredNtpServer']
        AlternateNtpServer = content['AlternateNtpServer']
        ExtraNtpServer = content['ExtraNtpServer']

        self.log_step("Step2: 修改NTPService信息")
        json_text = {"PreferredNtpServer": "ntp.jd.local", "AlternateNtpServer": "ntp.ark.jdcloud.com", "ExtraNtpServer": "bmctime.jd.com"}
        resp = self.request.patch(url=url, params=json_text)
        rc = resp[0]
        self.assertIsNotNone(rc)
        content = resp[1]
        self.logger.info(f"修改后回显信息：\n{json.dumps(content, indent=4)}")

        self.log_step("Step3: 再次查询NTPService信息")
        resp = self.request.get(url)
        if resp[0] != 200:
            self.assertIsNotNone(None, f"获取NTP信息错误:\n{json.dumps(resp[1], indent=4)}")
        content = resp[1]
        self.logger.info(json.dumps(content, indent=4))

        self.log_step("Step4: 恢复NTPService初始信息状态")
        json_text = {"PreferredNtpServer": PreferredNtpServer, "AlternateNtpServer": AlternateNtpServer, "ExtraNtpServer": ExtraNtpServer}

        header = self.request.get_header(url)
        if_match_str = header['ETag']
        self.request.headers['If-Match'] = if_match_str

        resp = self.request.patch(url=url, params=json_text)
        self.logger.info(json.dumps(json_text, indent=4))
        self.logger.info(json.dumps(resp, indent=4))
        rc = resp[0]
        self.assertIsNotNone(rc)
        content = resp[1]
        self.logger.info(f"修改后回显信息：\n{json.dumps(content, indent=4)}")

    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
